package com.sright.bcard.cms.controller;

import com.sright.bcard.cms.intercepter.LoginIntercepter;
import com.sright.bcard.common.service.share.dto.content.ContentUserInfoDTO;
import com.sright.bcard.common.service.share.dto.enterprise.AllAvatarDTO;
import com.sright.bcard.common.service.share.dto.enterprise.AllMaterialDTO;
import com.sright.bcard.common.service.share.dto.page.BetweenDateParamRequest;
import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.common.service.share.dto.request.CardEditRequest;
import com.sright.bcard.common.service.share.mvcresolver.BodyFiled;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.bcard.content.share.service.CardService;
import com.sright.bcard.content.share.service.CardUserInfoService;
import com.sright.bcard.enterprise.share.service.MaterialService;
import com.sright.common.response.SrightResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * Author: lumf
 * Date: 2020/8/18 2:09 PM
 */
@RestController
@Slf4j
public class MaterialManageController {
    @Autowired
    MaterialService materialService;

    @Autowired
    CardUserInfoService cardUserInfoService;

    @Autowired
    private CardService cardService;

    @EnableLoginAuth
    @GetMapping("/content/upload/list")
    public SrightResponse getMaterialList(
            @RequestParam(value = "content_type") Integer materialType,
            @RequestParam(value = "begin_time", required = false) Long beginTime,
            @RequestParam(value = "end_time", required = false) Long endTime,
            @RequestParam(value = "page") Integer pageIndex,
            @RequestParam(value = "pageSize") Integer pageSize,
            @RequestParam(value = "needReview", required = false) Integer needReview
    ) throws ParseException {
        PageParamRequest paramRequest = PageParamRequest.pageRequest(pageIndex, pageSize);
        BetweenDateParamRequest betweenDateParamRequest = BetweenDateParamRequest.request(beginTime, endTime);
        Pager<AllMaterialDTO> pager = materialService.pageList(materialType,
                betweenDateParamRequest,
                paramRequest,
                needReview
        );
        return SrightResponse.defaultSuccess(pager);
    }

    @EnableLoginAuth
    @GetMapping("/content/avatar/list")
    public SrightResponse getAvatarList(@RequestParam(value = "page") Integer pageIndex, @RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "needReview", required = false) Integer needReview
    )  {
        PageParamRequest paramRequest = PageParamRequest.pageRequest(pageIndex, pageSize);
        Pager<AllAvatarDTO> pager = materialService.avatarList(paramRequest, needReview);
        return SrightResponse.defaultSuccess(pager);
    }

    @EnableLoginAuth
    @PostMapping("/content/delete/avatar")
    public SrightResponse deleteAvatarById(@BodyFiled("id") Long id) {
        materialService.deleteAvatar(id);
        return SrightResponse.defaultSuccess();
    }

    @EnableLoginAuth
    @GetMapping("/content/list/cardUserInfo")
    public SrightResponse getCardUserInfoList(
            @RequestParam(value = "check_status", required = false) Integer checkStatus,
            @RequestParam(value = "begin_time", required = false) Long beginTime,
            @RequestParam(value = "end_time", required = false) Long endTime,
            @RequestParam(value = "page") Integer pageIndex,
            @RequestParam(value = "pageSize") Integer pageSize
            ) throws ParseException {
        PageParamRequest paramRequest = PageParamRequest.pageRequest(pageIndex, pageSize);
        BetweenDateParamRequest betweenDateParamRequest = BetweenDateParamRequest.request(beginTime, endTime);
        Pager<ContentUserInfoDTO> pager = cardUserInfoService.getCardUserInfoList(checkStatus, betweenDateParamRequest, paramRequest);
        return SrightResponse.defaultSuccess(pager);
    }

    @EnableLoginAuth
    @PostMapping("/content/delete/id")
    public SrightResponse deleteMaterialById(@BodyFiled("id") Long id) {
        return SrightResponse.defaultSuccess(materialService.deleteMaterial(id));
    }

    @EnableLoginAuth
    @PostMapping("/content/card_info/edit")
    public SrightResponse editCard(@RequestBody CardEditRequest cardEditRequest,@RequestAttribute LoginIntercepter.TokenInfo info) {
        return cardService.editCard(cardEditRequest,(long)info.getAdmin_id());
    }
}
